#!/bin/bash
# 健康检查脚本

set -e

# 检查Python模块是否可以导入
python3 -c "
import sys
import os
sys.path.insert(0, '/app')

try:
    from src.main import MigrationTool
    from src.parser import MySQLParser
    from src.converter import MySQLToKWDBConverter
    from src.performance import PerformanceProfiler
    from src.stability import StabilityManager
    print('✓ All modules imported successfully')
except ImportError as e:
    print(f'✗ Module import failed: {e}')
    sys.exit(1)

# 测试基本功能
try:
    parser = MySQLParser()
    test_sql = 'CREATE TABLE test (id INT PRIMARY KEY);'
    result = parser.parse_content(test_sql)
    if len(result) > 0:
        print('✓ Parser functionality working')
    else:
        print('✗ Parser test failed')
        sys.exit(1)
except Exception as e:
    print(f'✗ Parser test failed: {e}')
    sys.exit(1)

print('✓ Health check passed')
"

# 检查磁盘空间
DISK_USAGE=$(df /app | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 90 ]; then
    echo "✗ Disk usage too high: ${DISK_USAGE}%"
    exit 1
fi
echo "✓ Disk usage OK: ${DISK_USAGE}%"

# 检查内存使用
MEMORY_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100.0}')
if [ "$MEMORY_USAGE" -gt 90 ]; then
    echo "✗ Memory usage too high: ${MEMORY_USAGE}%"
    exit 1
fi
echo "✓ Memory usage OK: ${MEMORY_USAGE}%"

echo "🎉 All health checks passed!"
exit 0
